/*
 *所有关于TypeSetup类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*TypeSetupService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MyMES.IRepositories;
using MyMes.Infrastructure;
using System.Threading.Tasks;
using VOL.Core.Utilities.Response;
using MyMES.Dto;
using VOL.Entity.DomainModels.Core;
using System;

namespace MyMES.Services
{
    public partial class TypeSetupService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IBaseRepository<TypeSetup> _typesetupRepo;
        private readonly ITypeSetupRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public TypeSetupService(
            ITypeSetupRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            IBaseRepository<TypeSetup> typesetupRepo
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _typesetupRepo = typesetupRepo;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        
        /// <summary>
        /// 添加类型设置
        /// </summary>
        /// <param name="typeSetup">类型设置实体</param>
        /// <returns>操作结果</returns>
        public async Task<WebResponseContent> AddTypeSetup(TypeSetup typeSetup)
        {
            var res = new WebResponseContent();
            
            var i = await _typesetupRepo.AddAsync(typeSetup);
            
            res.OK("新增成功");
            
            return await Task.FromResult(res);
        }
        
        /// <summary>
        /// 获取类型设置列表
        /// </summary>
        /// <param name="dto">查询条件</param>
        /// <returns>操作结果</returns>
        public async Task<WebResponseContent> GetTypeSetup(GetTypeSetupDTO dto)
        {
            var res = new WebResponseContent();
            
            try
            {
                // 构建分页查询
                var query = _typesetupRepo.GetAll();
                
                // 计算总记录数
                int total = await query.CountAsync();
                
                // 分页并排序
                var data = await query
                    .OrderByDescending(x => x.Id)
                    .Skip((dto.PageIndex - 1) * dto.PageSize)
                    .Take(dto.PageSize)
                    .ToListAsync();
                
                // 构造返回结果
                var pageData = new PageGridData<TypeSetup>
                {
                    rows = data,
                    total = total,
                    status = 1
                };
                
                res.OK("查询成功", pageData);
            }
            catch (Exception ex)
            {
                res.Error($"查询失败：{ex.Message}");
            }
            
            return res;
        }
        
        /// <summary>
        /// 更新类型设置
        /// </summary>
        /// <param name="typeSetup">类型设置实体</param>
        /// <returns>操作结果</returns>
        public async Task<WebResponseContent> UpdTypeSetup(TypeSetup typeSetup)
        {
            var res = new WebResponseContent();
            
            var i = await _typesetupRepo.UpdateAsync(typeSetup);
            
            res.OK("修改成功");
            
            return await Task.FromResult(res);
        }

        /// <summary>
        /// 删除类型设置
        /// </summary>
        /// <param name="id">类型编码</param>
        /// <returns>操作结果</returns>
        public async Task<WebResponseContent> RemTypeSetup(string id)
        {
            var res = new WebResponseContent();
            
            var i = await _typesetupRepo.DelAll(x => id.Contains(x.Id.ToString()));
            
            res.OK("删除成功");
            
            return await Task.FromResult(res);
        }
    }
}
